package com.example.portal.controller;

import com.example.portal.model.ReturnVO;
import com.example.portal.model.SVNModel;
import com.example.portal.utils.SvnUtil;
import com.example.portal.pojo.SVNQO;
import com.example.portal.service.SvnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.File;
import java.util.List;

@RequestMapping("/svn")
@RestController
public class SvnController {
    private static String svnurl = "https://43.240.130.100/";
//    private static String svnurl = "svn://192.168.202.105/";
    private static String username = "svnportal";
//    private static String username = "liudong1";
    private static String password = "svnportal";
//    private static String password = "123456";
    private static String localurl = "D:/localSVN";
    @Autowired
    SvnService svnService;

    @RequestMapping(value = "/test-out", method = RequestMethod.GET)
//    @RequestMapping(value = "/test-out", method = RequestMethod.POST)
//    public ReturnVO test(@RequestBody @Validated SVNQO svnqo) {
    public ReturnVO test() {
//        String url = svnurl + svnqo.getPath();
//        System.out.println("svn获取路径：" + url);
//        SvnUtil svn = new SvnUtil(username,password,url);
//        SVNDirEntry data;
//        try {
//            data =  svn.getSVNInfo(url);
//        }
//        catch (SVNException ex) {
//            throw new RuntimeException(ex);
//        }
//        return new ReturnVO(data);
        return new ReturnVO(svnurl);
    }

    @RequestMapping(value = "/get-total", method = RequestMethod.POST)
    public ReturnVO getFileToString(@RequestBody @Validated SVNQO svnqo) {
        Integer total =0;
        return new ReturnVO(total);
    }

    @RequestMapping(value = "/browser-out", method = RequestMethod.POST)
    public ReturnVO getRepository(@RequestBody @Validated SVNQO svnqo) {
        String url = svnurl + svnqo.getPath();
        System.out.println("svn获取路径：" + url);
        SvnUtil svn = new SvnUtil(username,password,url);
        List<SVNModel> data;
        try {
            data = svn.getSVNSimplifyData(1);
        }
        catch (SVNException ex) {
            throw new RuntimeException(ex);
        }
        return new ReturnVO(data);

    }

    @RequestMapping(value = "/check-out", method = RequestMethod.GET)
    public String checkOut(Model model) {
            //初始化支持svn://协议的库。 必须先执行此操作。

                 System.out.println("svn");
            SVNRepositoryFactoryImpl.setup();

            //相关变量赋值
            SVNURL repositoryURL = null;
            try {
                repositoryURL = SVNURL.parseURIEncoded(svnurl);
            } catch (Exception e) {
                //
            }
            String name = "liudong1";//用户名
            String password = "123456";//密码
            ISVNOptions options = SVNWCUtil.createDefaultOptions(true);

            //实例化客户端管理类
            SVNClientManager ourClientManager = SVNClientManager.newInstance(
                    (DefaultSVNOptions) options, name, password);

            //要把版本库的内容check out到的目录
            File wcDir = new File(localurl);

            long workingVersion = -1;
            try {
                workingVersion = SvnUtil.checkout(ourClientManager, repositoryURL, SVNRevision.HEAD, wcDir, SVNDepth.INFINITY);
            } catch (Exception e) {
                e.printStackTrace();
            }
            model.addAttribute("msg", "把版本：" + workingVersion + " check out 到目录：" + wcDir + "中。");
            System.out.println("把版本：" + workingVersion + " check out 到目录：" + wcDir + "中。");
            return "msg";
    }
}
